package com.nbgroup.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.nbgroup.annotation.HttpMethod;
import com.nbgroup.pojo.Dict;
import com.nbgroup.pojo.DictData;
import com.nbgroup.pojo.common.PageBean;
import com.nbgroup.pojo.common.Result;
import com.nbgroup.pojo.dto.DictDTO;
import com.nbgroup.pojo.dto.DictPageQueryDTO;
import com.nbgroup.pojo.dto.PageQueryDTO;
import com.nbgroup.pojo.vo.DictVO;
import com.nbgroup.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-26
 */
@Api(tags = "字典接口")
@RestController
@RequestMapping("/system/dict/type")
public class DictController {

    @Autowired
    private IDictService dictService;

    /**
     * 分页查询
     */
    @HttpMethod("GET")
    @GetMapping("/list")
    @ApiOperation("字典分页查询")
    public PageBean PageQuery(DictPageQueryDTO dto){
        try {
            dto.check();
        } catch (Exception e) {
            List<Dict> list = dictService.list();
            Integer size = list.size();
            PageBean pageBean = PageBean.builder().total(Long.parseLong(size.toString()))
                    .msg("查询成功")
                    .code(200)
                    .rows(list)
                    .build();
            return pageBean;
        }
        Map<String, Object> params = dto.getParams();
        if (params == null){
            params = Map.of();
        }
        Page<Dict> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<Dict> p = dictService.lambdaQuery()
                .like(dto.getDictName() != null && !dto.getDictName().equals(""),Dict::getDictName, dto.getDictName())
                .like(dto.getDictType() != null && !dto.getDictType().equals(""),Dict::getDictType, dto.getDictType())
                .eq(dto.getStatus() != null,Dict::getStatus, dto.getStatus())
                .ge(params.containsKey("beginTime"), Dict::getCreateTime,params.get("beginTime"))
                .le(params.containsKey("endTime"), Dict::getCreateTime, params.get("endTime"))
                .orderBy(true,false,Dict::getCreateTime)
                .page(page);
        List<Dict> records = p.getRecords();
        List<DictVO> dictVOS = BeanUtil.copyToList(records, DictVO.class);
        PageBean pageBean = PageBean.builder().total(p.getTotal()).rows(dictVOS)
                .msg("查询成功")
                .code(200)
                .build();
        return pageBean;
    }

    /**
     *新增字典
     */
    @HttpMethod("POST")
    @PostMapping
    @ApiOperation("新增字典")
    public Result add(@RequestBody @Validated DictDTO dto){
        Dict dict = BeanUtil.copyProperties(dto, Dict.class);
        dictService.save(dict);
        return Result.success();
    }

    /**
     * 删除字典
     */
    @DeleteMapping("/{dictId}")
    @ApiOperation("删除字典")
    public Result delById(@PathVariable Long dictId){
        dictService.delete(dictId);
        return new Result();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{dictId}")
    public Result getById(@PathVariable Long dictId){
        Dict dict = dictService.getById(dictId);
        DictVO dictVO = BeanUtil.copyProperties(dict, DictVO.class);
        return Result.success(dictVO);
    }

    /**
     * 修改字典
     */
    @PutMapping
    public Result update(@RequestBody @Validated DictDTO dto){
        Dict dict = BeanUtil.copyProperties(dto, Dict.class);
        dictService.updateById(dict);
        return new Result();
    }
}
